{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "0bd32972-13e8-475c-9415-c50a50409568",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-09-04T14:17:41.716630Z",
     "iopub.status.busy": "2023-09-04T14:17:41.716258Z",
     "iopub.status.idle": "2023-09-04T14:17:42.097933Z",
     "shell.execute_reply": "2023-09-04T14:17:42.097255Z",
     "shell.execute_reply.started": "2023-09-04T14:17:41.716610Z"
    }
   },
   "source": [
    "### clone代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e35e35f2-89e4-4243-8c6c-bf3abe8b422c",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "!git clone https://github.com/modelscope/modelscope-agent.git"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "cdf9e03b-a513-4b81-a75e-243f38a3c84a",
   "metadata": {},
   "source": [
    "### 安装特定依赖"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3851d799-7162-4e73-acab-3c13cb1e43bd",
   "metadata": {
    "ExecutionIndicator": {
     "show": true
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "! cd modelscope-agent && ! pip install -r requirements.txt\n",
    "! pip install transformers_stream_generator\n",
    "! pip install invisible-watermark>=2.0\n",
    "! pip install accelerate --upgrade\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2c1ede08-857f-4592-9093-d7e5a37ce245",
   "metadata": {
    "ExecutionIndicator": {
     "show": true
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "import os\n",
    "os.chdir('modelscope-agent/demo')\n",
    "\n",
    "\n",
    "import sys\n",
    "sys.path.append('../')"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "325572f8-7960-479b-a91d-052a417b66ae",
   "metadata": {
    "tags": []
   },
   "source": [
    "### 中枢大模型启动"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ed061825-7e6a-4edd-bc27-ffb97b168681",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from modelscope_agent.llm import LLMFactory\n",
    "from modelscope.hub.api import HubApi\n",
    "\n",
    "model_name = 'modelscope-agent-7b'\n",
    "model_cfg = {\n",
    "    'modelscope-agent-7b':{\n",
    "        'type': 'modelscope',\n",
    "        'model_id': 'damo/ModelScope-Agent-7B',\n",
    "        'model_revision': 'v1.0.0',\n",
    "        'use_raw_generation_config': True,\n",
    "        'custom_chat': True\n",
    "    }\n",
    "}\n",
    "\n",
    "\n",
    "llm = LLMFactory.build_llm(model_name, model_cfg)\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "32d7bd5f-2df4-436c-b92c-75b625479379",
   "metadata": {},
   "source": [
    "### 新工具注册和Agent构建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5f65059d-9a54-42b8-bb26-7f320e3bfbc0",
   "metadata": {
    "ExecutionIndicator": {
     "show": true
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "from modelscope_agent.agent import AgentExecutor\n",
    "from modelscope_agent.tools import Tool\n",
    "\n",
    "class AliyunRenewInstanceTool(Tool):\n",
    "    description = '续费一台包年包月ECS实例'\n",
    "    name = 'RenewInstance'\n",
    "    parameters: list = [{\n",
    "        'name': 'instance_id',\n",
    "        'description': 'ECS实例ID',\n",
    "        'required': True\n",
    "    }, {\n",
    "        'name': 'period',\n",
    "        'description': '续费时长以月为单位',\n",
    "        'required': True\n",
    "    }]\n",
    "\n",
    "    def _local_call(self, *args, **kwargs):\n",
    "        instance_id = kwargs['instance_id']\n",
    "        period = kwargs['period']\n",
    "        return {'result': f'已完成ECS实例ID为{instance_id}的续费，续费时长{period}月'}\n",
    "\n",
    "# 新增工具可参考AliyunRenewInstanceTool构建方式，配置相关的api name，description等\n",
    "additional_tool_list = {\n",
    "    'RenewInstance': AliyunRenewInstanceTool()\n",
    "}\n",
    "# 构建Agent，需要传入llm，工具配置config以及工具检索\n",
    "\n",
    "agent = AgentExecutor(llm, additional_tool_list=additional_tool_list, tool_retrieval=False)\n",
    "available_tool_list = ['RenewInstance']\n",
    "agent.set_available_tools(available_tool_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7eed2650-f9ba-4d09-86d1-58e24ab27b38",
   "metadata": {
    "ExecutionIndicator": {
     "show": true
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 重置对话，清空对话历史\n",
    "agent.reset()\n",
    "\n",
    "# remote=True为调用modelscope api，该服务免费支持QPS较低，建议部署在本地，将remote=False\n",
    "agent.run(\"请帮我续费一台ECS实例，实例id是：i-rj90a7e840y5cde，续费时长10个月\", remote=False, print_info=True)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
